package com.yan.demo2;

import javax.crypto.CipherInputStream;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author taoge
 * @Date 2024/8/9 9:56
 * @Description
 */
public class Demo2 {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(108, new Runnable() {
            @Override
            public void run() {
                System.out.println("集齐108将兑换小霸王");
            }
        });

        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() +"已收集");

                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        };

        for (int i = 1; i <= 108; i++) {
            new Thread(runnable , "梁山好汉" + i).start();
        }
    }
}
